Skip to content

mismatchingContainers: detect mismatched iterator ranges passed to container member functions - #9042

Open
tropibyte wants to merge 1 commit into
cppcheck-opensource:mainfrom
tropibyte:mismatching-range-insert
Open

tropibyte wants to merge 1 commit into
cppcheck-opensource:mainfrom
tropibyte:mismatching-range-insert

Conversation

@tropibyte

Copy link
Copy Markdown
  • Checks iterator ranges passed to insert, assign, append and replace of a container, so ints2.insert(it1, it2) with iterators from different containers is now reported. insert(hint, value) on associative containers, where the first iterator belongs to the container itself, is excluded.
  • Library container detection now recognizes nested iterator types of non-template containers and std::span (std::string::iterator, std::span<int>::iterator), so such variables get an iterator value type. std::span<int>::size_type is no longer treated as a container.
  • Replaces the stale TODO in CheckStl::iterators() with a pointer to mismatchingContainers(), which now does that check.
  • Tests: converts the TODO_ASSERT in TestStl::iterator6 and five in TestLibrary, and adds positive and negative cases.

There is no Trac ticket for this yet. Could one of you file one? Once it exists I'll add Fix #NNNNN to the commit title.

…ntainer member functions

Iterator ranges passed to insert(), assign(), append() and replace() of a
container were not checked, so 'ints2.insert(it1, it2)' with iterators from
two different containers went unreported (TODO in TestStl::iterator6).

To support this, library container detection now recognizes nested iterator
types of non-template containers and of std::span, e.g. std::string::iterator
and std::span<int>::iterator. The stale TODO in CheckStl::iterators() now
points at mismatchingContainers(), which performs the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 33a5aec (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9042/
packages: 100
Differing warnings: 1 (+1 -0)
AI review: 1 improvement (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9042/2026-11-17_16-09-17-33a5aecf9a28-review.txt)
Timing: +2.5% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9042/2026-11-17_16-09-17-33a5aecf9a28_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@dmcppcheck

Copy link
Copy Markdown
Contributor

There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The new check works on every case I tried, and I found no false positive or crash in mismatchingContainers.

One new problem: variables of type std::string::iterator now get an iterator value type. Because of that, std::string parameters now get the same constParameterReference false positive that std::vector already gets. std::string::iterator it = s.begin(); makes cppcheck say that s can be const std::string&, but with a const string begin() returns a const_iterator, so that code would not compile. The base version does not report this for strings.

Nothing else that is new: the unit tests pass, and the cfg tests and the files I checked from cppcheck's own sources give the same results as base.

Comments

# Comment By Where Status
1 std::string::iterator detection adds constParameterReference false positives AI review cfg/std.cfg:9109 🆕 New

@tropibyte, please fix, resolve or reply to the open and new comments: #1.

Testing

Built cppcheck and testrunner in pr/, and cppcheck in base/.

  • ./testrunner TestStl TestLibrary TestSymbolDatabase TestValueFlow TestAutoVariables: 0 failures.
  • test/cfg/std.cpp, qt.cpp, wxwidgets.cpp and microsoft_gsl.cpp checked with both binaries: same output.
  • lib/mathlib.cpp, lib/utils.cpp and lib/library.cpp checked with both binaries: same output.

mismatchingContainers cases (scratch):

  • Found as expected:
    • l.insert(l.end(), a.begin(), b.end()) and l.assign(a.begin(), b.end()) on std::list
    • s.replace(s.begin(), s.end(), a.begin(), b.end()) with vectors as the source
    • iterator variables, e.g. first = a.begin(); last = v.end(); v.insert(v.begin(), first, last)
  • No warning, as expected:
    • insert(hint, value) on set<vector<int>::iterator>, with the hint from a parameter, s.find(..), std::next(s.begin()), std::prev(s.end()), ps->end(), m[1].end(), vs[i].end(), b.s.end(), this->s.end(), a reference alias, or h = s.insert(h, ..) in a loop
    • insert(pos, n, it) and assign(n, it) on vector<iterator>
    • conditional ranges like c ? a.begin() : s.begin()

New false positive (reproduced):

#include <string>
int f(std::string& s) {
    std::string::iterator it = s.begin();
    return *it;
}

cppcheck -q --enable=style c1.cpp

  • base: no output
  • PR: c1.cpp:3:20: style: Parameter 's' can be declared as reference to const [constParameterReference]

The same pattern with std::vector<int>& already gives this warning in base. The PR makes strings behave the same way, and the warning is wrong for both, because a const_iterator can not be converted to iterator. A wider comparison of std::string code showed the same new warning in a loop with for (std::string::iterator it = s.begin(); ...).


Automated review of 33a5aec by Claude. The pull request was built and tested locally.

Comment thread cfg/std.cfg
<type templateParameter="0"/>
</container>
<container id="stdString" startPattern="std :: string|wstring|u16string|u32string" endPattern="" inherits="stdAllString"/>
<container id="stdString" startPattern="std :: string|wstring|u16string|u32string" endPattern="" itEndPattern=":: iterator|const_iterator|reverse_iterator|const_reverse_iterator" inherits="stdAllString"/>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

With this itEndPattern, std::string::iterator variables become iterators, and that brings a new constParameterReference false positive for std::string& parameters:

int f(std::string& s) {
    std::string::iterator it = s.begin();
    return *it;
}

PR: c1.cpp:3:20: style: Parameter 's' can be declared as reference to const [constParameterReference]. Base: no warning.

If s is const, s.begin() returns a const_iterator, which can not be converted to std::string::iterator, so following the suggestion breaks the build. std::vector<int>::iterator already has the same false positive in base. Since this PR spreads it to every string, the const check should treat an initialization of a non-const iterator variable from begin()/end() etc. as a non-const use. At least add a test for this case.

@chrchr-github

Copy link
Copy Markdown
Collaborator

I have created https://trac.cppcheck.net/ticket/15232 for the newly discovered constParameterReference issue.

@chrchr-github

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants